package com.longjianghuasheng.logistics.business.grab_an_order

import android.os.Bundle
import androidx.lifecycle.viewModelScope
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.databinding.FragmentGrabASingleDetailsBinding
import com.longjianghuasheng.logistics.entity.DriverFormListItem
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.loadUrl
import com.longjianghuasheng.logistics.ext.observer
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.view.visible

class GrabASingleDetailsFragment :
    BaseTitleFragment<GrabASingleDetailsViewModel, FragmentGrabASingleDetailsBinding>() {

    override fun layoutId(): Int = R.layout.fragment_grab_a_single_details

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initTitleBar("抢单详情")
        mDatabind.btnGrabASingle.setOnClickListener {
            mViewModel.driver_do()
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {

            mDatabind.setData(it)
        }
        mViewModel.notify.observer(this) {

            mViewModel.viewModelScope.launch {
                eventViewModel.onGrabASingleUpdateEvent.value = mViewModel.id
                delay(300L)

                navigateUp()
            }
        }
    }

    private fun FragmentGrabASingleDetailsBinding.setData(it: DriverFormListItem) {
        it.run {
            ivPic.loadUrl(picurl)
            stvStart.setCenterString(start_address)
            stvEnd.setCenterString(end_address)
            stvName.setCenterString(names)
            stvNum.setCenterString(nums)
            stvWeight.setCenterString(weights)
            stvVolume.setCenterString(volume)
            stvMoney.setCenterString("￥$price")
            stvCreateTime.setCenterString(create_time)
        }


        llContainer0.visible()

    }

    override fun initData() {
        super.initData()
        mViewModel.id = arguments?.getInt("id") ?: 0
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.driver_form_show()
    }
}